import Ice
import traceback
import sys


from asterisk.bridge import loggerPrx, channelPrx

class Conn:
	
	def __init__(self,(host,port)=('localhost','6543')):
		self.host=host
		self.port=port
		self.communicator = Ice.initialize(sys.argv)
	
	def __call__(self,name,obj):
		
		return obj(name,self.communicator,(self.host,self.port))

class Bridge:
	
	def __init__(self,name,communicator,(host,port)):	
		self.name = name
		self.communicator = communicator
		self.proxy = communicator.stringToProxy("%s:default -h %s -p %s" % (self.name,host,port))
		
		
		
class Channel(Bridge):

	def __init__(self,name,communicator,(host,port)):
		Bridge.__init__(self,name,communicator,(host,port))
		self.link = channelPrx.checkedCast(self.proxy)
	
	
	def getbyname(self,name):
		return self.link.getbyname(name)
		
	def getbyexten(self,exten):
		return self.link.getbyexten(exten)
		
	def get(self):
		return self.link.get()
		
class Logger(Bridge):
	
	debug	= 0
	event	= 1
	notice	= 2
	warning	= 3
	error	= 4
	verbose	= 5
	dmtf	= 6
	
	def __init__(self,name,communicator,(host,port)):
		Bridge.__init__(self,name,communicator,(host,port))
		self.link = loggerPrx.checkedCast(self.proxy)
	
			
	def prompt(self,level,message):
		stack =  traceback.extract_stack()
		stack = stack[len(stack)-2] 
		self.link.prompt(level,stack[0],stack[1],stack[2],"%s" % message)		 
	
